package com.example.ffepeg.ffeAdder;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FfeImg {

    public static void main(String[] args) {
        //去掉图片水印
      /*  String inputImgPath = "src/main/resources/file/logo/outImageMp4.png";
        int startX = 125; // X坐标
        int startY = 530;  // Y坐标
        int watermarkWidth = 160;  // 水印宽度
        int watermarkHeight = 30; // 水印高度
        String outputImgPath = "src/main/resources/out/output.png";
        boolean b = removeVideoOrImgWatermark(inputImgPath, startX, startY, watermarkWidth, watermarkHeight, outputImgPath);*/


        //去掉视频水印
        String inputImgPath = "https://pushimg.liecaizhijia.com/ai_education/mapImg/2025-02-26/2be6a0a1-3261-4b39-960a-b772605224e6.mp4";
        int startX = 125; // X坐标
        int startY = 150;  // Y坐标
        int watermarkWidth = 160;  // 水印宽度
        int watermarkHeight = 25; // 水印高度
        String outputImgPath = "D:\\javaXM\\FFEpeg\\src\\main\\resources\\out\\outputng.mp4";
        boolean b = removeVideoOrImgWatermark(inputImgPath, startX, startY, watermarkWidth, watermarkHeight, outputImgPath);

    }

    public static boolean removeVideoOrImgWatermark(String inputVideoUrl, int startX, int startY, int watermarkWidth, int watermarkHeight, String outputVideoPath) {
        // 构建FFmpeg命令
        String[] command = {
                "ffmpeg",
                "-i", inputVideoUrl,
                "-vf", "delogo=x=" + startX + ":y=" + startY + ":w=" + watermarkWidth + ":h=" + watermarkHeight,
                "-c:a", "copy",
                outputVideoPath
        };

        // 使用ProcessBuilder执行命令
        ProcessBuilder processBuilder = new ProcessBuilder(command);
        processBuilder.redirectErrorStream(true); // 将错误流合并到标准输出流
        try {
            // 启动进程
            Process process = processBuilder.start();
            // 读取FFmpeg的输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            // 等待进程完成
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("去掉视频水印成功");
                return true;
            } else {
                System.out.println("去掉视频水印失败: " + exitCode);
                throw new RuntimeException("去掉视频水印失败"); // 假设这里不再抛出特定的BusinessException
            }
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e.getMessage()); // 同样假设这里不再抛出特定的BusinessException
        }
    }


}
